package cn.edu.tyu.finance.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.tyu.finance.domain.Category;
import cn.edu.tyu.finance.util.DbUtil;

/**
 * Servlet implementation class FindByIdServlet
 */
@WebServlet("/FindByIdServlet")
public class FindByIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindByIdServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 实现按类别编号查询的功能
				//1. 接收网页上超链接的参数id
				String cid = request.getParameter("id");
				//2. 连上数据库，查询数据，按id查的，只能查到1条
				Connection conn = null;
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				Category ct = new Category();
				
				conn = DbUtil.getConnection();
				String sql = "SELECT * FROM category WHERE id=?";
				try {
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, cid);
					rs = pstmt.executeQuery();
					rs.next();
					ct.setId(rs.getString("id"));
					ct.setName(rs.getString("name"));
					ct.setRemark(rs.getString("remark"));
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					DbUtil.backConn(conn, pstmt, rs);
				}
				request.getSession().setAttribute("cate", ct);
				response.sendRedirect("modify.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
